home *** CD-ROM | disk | FTP | other *** search
- #ifndef __STD_STREAMBUF__
- #define __STD_STREAMBUF__
- #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
-
- /***************************************************************************
- *
- * streambuf - Declarations for the Standard Library stream buffers
- *
- * $Id: streambuf,v 1.52 1996/09/24 19:18:25 smithey Exp $
- *
- ***************************************************************************
- *
- * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
- * ALL RIGHTS RESERVED *
- * The software and information contained herein are proprietary to, and
- * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
- * intends to preserve as trade secrets such software and information.
- * This software is furnished pursuant to a written license agreement and
- * may be used, copied, transmitted, and stored only in accordance with
- * the terms of such license and with the inclusion of the above copyright
- * notice. This software and information or any other copies thereof may
- * not be provided or otherwise made available to any other person.
- *
- * Notwithstanding any other lease or license that may pertain to, or
- * accompany the delivery of, this computer software and information, the
- * rights of the Government regarding its use, reproduction and disclosure
- * are as set forth in Section 52.227-19 of the FARS Computer
- * Software-Restricted Rights clause.
- *
- * Use, duplication, or disclosure by the Government is subject to
- * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
- * Technical Data and Computer Software clause at DFARS 252.227-7013.
- * Contractor/Manufacturer is Rogue Wave Software, Inc.,
- * P.O. Box 2328, Corvallis, Oregon 97339.
- *
- * This computer software and information is distributed with "restricted
- * rights." Use, duplication or disclosure is subject to restrictions as
- * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
- * Computer Software-Restricted Rights (April 1985)." If the Clause at
- * 18-52.227-74 "Rights in Data General" is specified in the contract,
- * then the "Alternate III" clause applies.
- *
- **************************************************************************/
-
- #include <stdcomp.h>
-
- #ifndef _RWSTD_NO_NEW_HEADER
- #include <cstdlib>
- #else
- #include <stdlib.h>
- #endif
-
- #include <ios>
-
- #ifndef _RWSTD_NO_NAMESPACE
- namespace std {
- #endif
-
- template<class charT, class traits>
- class _RWSTDExportTemplate basic_streambuf {
-
- public:
-
- typedef charT char_type;
- typedef traits traits_type;
- typedef _TYPENAME traits::int_type int_type;
- typedef _TYPENAME traits::pos_type pos_type;
- typedef _TYPENAME traits::off_type off_type;
-
-
- virtual ~basic_streambuf();
-
- locale pubimbue( const locale& loc);
- locale getloc() const;
-
-
- inline basic_streambuf<char_type, traits> *
- pubsetbuf(char_type *s, streamsize n);
-
- inline pos_type pubseekoff(off_type off, ios_base::seekdir way,
- ios_base::openmode which =
- ios_base::in | ios_base::out);
-
- inline pos_type pubseekpos(pos_type sp, ios_base::openmode which =
- ios_base::in | ios_base::out);
-
- inline int pubsync( );
-
- inline ios_base::openmode which_open_mode();
-
- inline streamsize in_avail();
-
- inline int_type snextc();
-
- inline int_type sbumpc();
-
- inline int_type sgetc();
-
- inline streamsize sgetn(char_type *s, streamsize n);
-
- inline int_type sputbackc(char_type c);
-
- inline int_type sungetc();
-
- inline int_type sputc(char_type c);
-
- inline streamsize sputn(const char_type *s, streamsize n);
-
- #ifdef _RWSTD_MULTI_THREAD
- _RWSTDMutex buffer_mutex_;
-
- void _RW_lock_buffer()
- {
- _RWSTDGuard* tmp = new _RWSTDGuard(buffer_mutex_);
- if ( tmp )
- buffer_guard = tmp;
- else
- buffer_guard = 0;
- }
-
- void _RW_unlock_buffer()
- {
- if ( buffer_guard )
- {
- delete buffer_guard;
- buffer_guard = 0;
- }
- }
-
- #endif
-
- protected:
-
- basic_streambuf();
-
- ios_base::openmode mode_;
-
- inline char_type *eback() const;
- inline char_type *gptr() const;
- inline char_type *egptr() const;
-
- inline void gbump(int n);
-
- inline void setg(char_type *gbeg_arg,char_type *gnext_arg,char_type *gend_arg);
-
-
- inline char_type *pbase() const;
- inline char_type *pptr() const;
- inline char_type *epptr() const;
-
- inline void pbump(int n);
-
- inline void setp(char_type *pbeg_arg,char_type *pend_arg);
-
- virtual void imbue( const locale& loc);
-
- virtual int_type overflow(int_type c = traits::eof());
-
- virtual int_type pbackfail(int_type c = traits::eof());
-
- virtual int showmanyc();
-
- virtual int_type underflow();
-
- virtual int_type uflow();
-
- virtual streamsize xsgetn(char_type *s, streamsize n);
-
- virtual streamsize xsputn(const char_type *s, streamsize n);
-
- virtual pos_type seekoff(off_type off,
- ios_base::seekdir way, ios_base::openmode which =
- ios_base::in | ios_base::out);
-
- virtual pos_type seekpos(pos_type sp,
- ios_base::openmode which =
- ios_base::in | ios_base::out);
-
- virtual basic_streambuf<charT, traits> *
- setbuf(char_type *s, streamsize n);
-
-
- virtual int sync( );
-
- void streambuf_init(bool set_mode= true);
-
- private:
-
- char_type *gbeg_;
- char_type *gnext_;
- char_type *gend_;
-
- char_type *pbeg_;
- char_type *pnext_;
- char_type *pend_;
-
- locale loc_buf;
-
- #ifdef _RWSTD_MULTI_THREAD
- _RWSTDGuard *buffer_guard;
- #endif
- };
-
-
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- // inline functions
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- /*
- * int_type snextc()
- *
- * returns the next character
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::int_type
- basic_streambuf<charT, traits>::snextc()
- {
- if( traits::eq_int_type(sbumpc(),traits::eof()) )
- return traits::eof();
-
- return sgetc();
- }
-
- /*
- * int_type sbumpc()
- *
- * returns the current character and increments the pointer
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::int_type
- basic_streambuf<charT, traits>::sbumpc()
- {
- char_type c;
-
- if( gptr()>= egptr()) {
- return uflow();
- }
-
- c=*gptr();
- gbump(1);
- return traits::to_int_type(c);
- }
-
- /*
- * int_type sgetc()
- *
- * returns the current character
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::int_type
- basic_streambuf<charT, traits>::sgetc()
- {
-
- if(gptr() >= egptr()) {
- return underflow();
- }
-
- return traits::to_int_type(*gptr());
- }
-
- /*
- * streamsize sgetn(char_type *, streamsize)
- *
- * reads in 'n' characters into 's'
- */
-
- template<class charT, class traits>
- inline streamsize basic_streambuf<charT, traits>::
- sgetn(char_type *s, streamsize n)
- {
- return xsgetn(s, n);
- }
-
-
- /*
- * int_type sputbackc(char_type)
- *
- * puts the character back into the sequence if possible
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::int_type
- basic_streambuf<charT, traits>::sputbackc(char_type c)
- {
- if( gptr() > eback()) {
- gbump(-1);
- if( !traits::eq(*gptr(),c) ) {
- gbump(1);
- return pbackfail(traits::to_int_type(c));
- }
-
- return traits::to_int_type(*gptr());
- }
-
- return pbackfail(traits::to_int_type(c));
- }
-
-
- /*
- * int sungetc()
- *
- * puts the character back into the sequence. The putback character doesn't
- * have to already be there. Similar to sputbackc(...)
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::int_type
- basic_streambuf<charT, traits>::sungetc()
- {
- if(gptr() > eback())
- {
- gbump(-1);
- return traits::to_int_type(*gptr());
- }
-
- return pbackfail();
- }
-
- /*
- * int sputc(char_type)
- *
- * puts the character into the sequence
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::int_type
- basic_streambuf<charT, traits>::sputc(char_type c)
- {
-
- if( pptr() >= epptr() ) {
- return overflow(traits::to_int_type(c));
- }
-
- *pnext_++ =c;
- return traits::to_int_type(c);
- }
-
- /*
- * streamsize sputn(const char_type *, streamsize)
- *
- * writes n characters from s into the sequence
- */
-
- template<class charT, class traits>
- inline streamsize
- basic_streambuf<charT, traits>::sputn(const char_type *s, streamsize n)
- {
- return xsputn(s, n);
- }
-
- /*
- * void gbump(int)
- *
- * increments the get pointer by n. Does no checking to see if this
- * is a valid operation
- */
-
- template<class charT, class traits>
- inline void
- basic_streambuf<charT, traits>::gbump(int n)
- {
- gnext_ += n;
- }
-
- /*
- * void setg(char_type *, char_type *, char_type *)
- *
- * sets the get pointers
- */
-
- template<class charT, class traits>
- inline void
- basic_streambuf<charT, traits>::
- setg(char_type *gbeg, char_type *gnext, char_type *gend)
- {
- gbeg_ = gbeg;
- gnext_ = gnext;
- gend_ = gend;
- }
-
-
- /*
- * void pbump(int)
- *
- * increments the put pointer. No checking to see if this is valid
- */
-
- template<class charT, class traits>
- inline void
- basic_streambuf<charT, traits>::pbump(int n)
- {
- pnext_ += n;
- }
-
- /*
- * void setp(char_type *, char_type *)
- *
- * sets up the put pointers
- */
-
- template<class charT, class traits>
- inline void
- basic_streambuf<charT, traits>::
- setp(char_type *pbeg, char_type *pend)
- {
- pbeg_ = pbeg;
- pnext_ = pbeg;
- pend_ = pend;
- }
-
- /*
- * char_type *eback() const
- *
- * returns the beginning of the get sequence
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::char_type *
- basic_streambuf<charT, traits>::eback() const
- {
- return gbeg_;
- }
-
- /*
- * char_type *gptr() const
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::char_type *
- basic_streambuf<charT, traits>::gptr() const
- {
- return gnext_;
- }
-
- /*
- * char_type *egptr() const
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::char_type *
- basic_streambuf<charT, traits>::egptr() const
- {
- return gend_;
- }
-
- /*
- * char_type *pbase() const
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::char_type *
- basic_streambuf<charT, traits>::pbase() const
- {
- return pbeg_;
- }
-
- /*
- * char_type *pptr() const
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::char_type *
- basic_streambuf<charT, traits>::pptr() const
- {
- return pnext_;
- }
-
- /*
- * char_type *epptr() const
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::char_type *
- basic_streambuf<charT, traits>::epptr() const
- {
- return pend_;
- }
-
-
- /*
- * streamsize in_avail()
- *
- * returns how many characters are available
- */
-
- template<class charT, class traits>
- inline streamsize
- basic_streambuf<charT, traits>::in_avail()
- {
- if(gptr() >= egptr())
- return showmanyc();
-
- return ( (streamsize)(egptr() - gptr()) );
- }
-
-
- /*
- * int pubsync()
- */
-
- template<class charT, class traits>
- inline int
- basic_streambuf<charT, traits>::pubsync()
- {
- return sync();
- }
-
-
- /*
- * pos_type pubseekoff(off_type, ios_base::seekdir, ios_base::openmode)
- */
-
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::pos_type
- basic_streambuf<charT, traits>::
- pubseekoff(off_type off, ios_base::seekdir way, ios_base::openmode which)
- {
- return seekoff(off, way, which);
- }
-
- /*
- * pos_type pubseekpos(pos_type, ios_base::openmode)
- */
- template<class charT, class traits>
- inline _TYPENAME basic_streambuf<charT, traits>::pos_type
- basic_streambuf<charT, traits>::
- pubseekpos(pos_type sp,
- ios_base::openmode which)
- {
- return seekpos(sp, which);
- }
-
- /*
- * basic_streambuf *pubsetbuf(char_type *, streamsize)
- */
-
- template<class charT, class traits>
- inline basic_streambuf<charT, traits> *
- basic_streambuf<charT, traits>::
- pubsetbuf(char_type *s, streamsize n)
- {
- return setbuf(s, n);
- }
-
- /*
- * ios_base::openmode which_open_mode()
- */
-
- template<class charT, class traits>
- inline ios_base::openmode
- basic_streambuf<charT, traits>::which_open_mode()
- {
- return mode_;
- }
-
-
- // END OF INLINE FUNCTIONS
-
- /*
- * streambuf iterators
- */
-
- //
- // ostreambuf_iterator
- //
-
- template<class charT, class traits >
- class _RWSTDExportTemplate ostreambuf_iterator : public output_iterator {
-
- public:
-
- typedef charT char_type;
- typedef traits traits_type;
- typedef basic_streambuf<charT, traits> streambuf_type;
- typedef basic_ostream<charT, traits> ostream_type;
-
- ostreambuf_iterator(ostream_type& s) _RWSTD_INLINE_NO_THROW
- : sbuf_(s.rdbuf())
- {
- if ( s.rdbuf() ) failed_flag = false;
- else
- failed_flag = true;
- }
-
- ostreambuf_iterator(streambuf_type *s) _RWSTD_INLINE_NO_THROW
- : sbuf_(s)
- {
- if ( s ) failed_flag = false;
- else
- failed_flag = true;
- }
-
- ostreambuf_iterator<charT,traits>& operator*()
- { return *this; }
- ostreambuf_iterator<charT,traits>& operator++()
- { return *this; }
- ostreambuf_iterator<charT,traits> operator++(int)
- { return *this; }
-
- ostreambuf_iterator<charT,traits>& operator=(charT c)
- {
- if ( !failed_flag )
- {
- if ( traits::eq_int_type(sbuf_->sputc(c),traits::eof()) )
- failed_flag=true;
- }
- return *this;
- }
-
- bool failed( ) const _RWSTD_INLINE_NO_THROW
- { return failed_flag; }
-
-
- protected:
-
- private:
-
- streambuf_type *sbuf_;
- bool failed_flag;
-
- };
-
- //
- // istreambuf_iterator
- //
-
- template<class charT, class traits >
- class _RWSTDExportTemplate istreambuf_iterator : public input_iterator<charT>
- {
-
- public:
-
- typedef charT char_type;
- typedef _TYPENAME traits::int_type int_type;
- typedef traits traits_type;
- typedef basic_streambuf<charT, traits> streambuf_type;
- typedef basic_istream<charT, traits> istream_type;
-
- // class to maintain a character and an associated streambuf
- class proxy {
-
- char_type keep_;
- streambuf_type *sbuf_;
-
- proxy(char_type c, streambuf_type *sbuf)
- : keep_(c), sbuf_(sbuf)
- { ; }
-
- public:
-
- char_type operator*()
- { return keep_; }
-
- friend class istreambuf_iterator<charT, traits>;
-
- };
-
- public:
-
- istreambuf_iterator() _RWSTD_INLINE_NO_THROW
- : sbuf_(0)
- { failed_flag = true; }
- istreambuf_iterator(istream_type& s) _RWSTD_INLINE_NO_THROW
- : sbuf_(s.rdbuf())
- {
- if ( s.rdbuf() ) failed_flag = false;
- else
- failed_flag = true;
- }
- istreambuf_iterator(streambuf_type *s) _RWSTD_INLINE_NO_THROW
- : sbuf_(s)
- {
- if ( s ) failed_flag = false;
- else
- failed_flag = true;
- }
- istreambuf_iterator(const proxy& p) _RWSTD_INLINE_NO_THROW
- : sbuf_(p.sbuf_)
- { ; }
- inline char_type operator*();
- inline istreambuf_iterator<charT, traits>& operator++();
- inline proxy operator++(int);
- inline bool equal(istreambuf_iterator<charT, traits>& b);
-
- bool failed( ) const _RWSTD_INLINE_NO_THROW
- { return failed_flag; }
-
- protected:
-
- private:
-
- streambuf_type *sbuf_;
- bool failed_flag;
-
- };
-
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++
- // inline functions
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- /*
- * char_type operator*()
- */
-
- template<class charT, class traits>
- inline _TYPENAME istreambuf_iterator<charT, traits>::char_type
- istreambuf_iterator<charT, traits>::operator*()
- {
- int_type c;
-
- if ( sbuf_ && !failed_flag )
- {
- c= sbuf_->sgetc();
- if ( traits::eq_int_type(c,traits::eof()) )
- {
- sbuf_ = 0;
- failed_flag = true;
- }
- }
- else return traits::eof();
-
- return traits::to_char_type(c);
-
- }
-
- /*
- * istreambuf_iterator& operator++()
- */
-
- template<class charT, class traits>
- inline istreambuf_iterator<charT, traits>&
- istreambuf_iterator<charT, traits>::operator++()
- {
- if (sbuf_ && !failed_flag )
- {
- sbuf_->sbumpc();
- if ( traits::eq_int_type(sbuf_->sgetc(),traits::eof()) )
- {
- sbuf_ = 0;
- failed_flag = true;
- }
- }
-
- return *this;
- }
-
- /*
- * proxy operator++(int)
- */
-
- template<class charT, class traits>
- inline _TYPENAME istreambuf_iterator<charT, traits>::proxy
- istreambuf_iterator<charT, traits>::operator++(int)
- {
-
- if (sbuf_ && !failed_flag )
- {
- proxy prev(sbuf_->sgetc(), sbuf_);
- sbuf_->sbumpc();
- if ( traits::eq_int_type(sbuf_->sgetc(),traits::eof()) )
- {
- sbuf_ = 0;
- failed_flag = true;
- }
- return prev;
- }
-
- charT c=traits::to_char_type(traits::eof());
- return proxy(c, sbuf_);
-
-
- }
-
- /*
- * bool equal(istreambuf_iterator&)
- */
-
- template<class charT, class traits>
- inline bool
- istreambuf_iterator<charT, traits>::
- equal(istreambuf_iterator<charT, traits>& b)
- {
- if( ((sbuf_ ==0) && (b.sbuf_==0)) || ((sbuf_ !=0) && (b.sbuf_ !=0)) )
- return true;
- else
- return false;
- }
-
-
- /*
- * bool operator==(istreambuf_iterator& a, istreambuf_iterator& b)
- */
-
- template<class charT, class traits>
- inline bool _RWSTDExportTemplate operator==(istreambuf_iterator<charT, traits>& a,
- istreambuf_iterator<charT, traits>& b)
- {
- return a.equal(b);
- }
-
-
- /*
- * bool operator!=(istreambuf_iterator& a, istreambuf_iterator& b)
- */
-
- template<class charT, class traits>
- inline bool _RWSTDExportTemplate operator!=(istreambuf_iterator<charT, traits>& a,
- istreambuf_iterator<charT, traits>& b)
- {
- return !(a.equal(b));
- }
-
- // end inlining
-
-
-
- #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
- typedef basic_streambuf<char> streambuf;
- #else
- typedef basic_streambuf<char, char_traits<char> > streambuf;
- #endif
-
- #ifndef _RWSTD_NO_WIDE_CHAR
- #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
- typedef basic_streambuf<wchar_t> wstreambuf;
- #else
- typedef basic_streambuf<wchar_t, char_traits<wchar_t> > wstreambuf;
- #endif
- #endif
-
-
- #ifndef _RWSTD_NO_NAMESPACE
- }
- #endif
-
-
-
- #ifdef _RWSTD_COMPILE_INSTANTIATE
- #include <streambu.cc>
- #endif
-
- #ifndef __USE_STD_NAMES__
- using namespace std;
- #endif
-
- #pragma option pop
-
- #endif /* __STREAMBUF__ */
-